<!-- 用户管理 -->
<template>
  <div>
    <el-main class="base_main_content">
      <div class="base_screen_header">
        <el-button type="primary" @click="addUser">新增用户</el-button>
        <el-form :inline="true" class="base_search_form" @submit.prevent>
          <el-form-item>
            <el-select v-model="base" @change="searchBase" placeholder="请选择基地" clearable>
              <el-option v-for="(item, index) in baseOptions" :key="index" :label="item.label" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="name" @keyup[native].enter="searchName" @change="searchName" placeholder="账号" clearable :suffix-icon="Search" />
          </el-form-item>
        </el-form>
      </div>
      <StatusBar :data="statusList" :default="2" />
      <div class="base_main_header">
        <el-button type="primary" text @click="userExport">导出</el-button>
      </div>
      <DataCollectTable :data="data" :total="total" :config="config" @operationsHander="opHander.receive">
      </DataCollectTable>
    </el-main>
  </div>
</template>

<script setup>
import StatusBar from '@com/StatusBar.vue';
import DataCollectTable from '@com/table/DataCollectTable.vue'
import { OP_KEY, COLUMN_TYPE, OPERATIONS, operationsHander } from "@com/table/tableOperations";
import { ref, reactive } from 'vue'
import { Search } from '@element-plus/icons-vue'
import useRoute from "@util/router";
import { $msg } from "@ui/msg"
import $ from "@util/base"
import api from "@api/user"

const route = useRoute()

route.watchRouter('usermanager', () => { getUserList() })

const baseOptions = ref([])

getConfig()
function getConfig() {
  api.base(val => {
    baseOptions.value = val
  })
}

const statusList = reactive([{
  label: '全部',
  value: 2,
  total: 0
}, {
  label: '正常',
  value: 1,
  total: 0
}, {
  label: '封禁',
  value: 0,
  total: 0
}])

const base = ref(route.query().base || '')
const name = ref(route.query().name || '')

function searchBase(val) {
  route.replaceQuery('base', val)
}

function searchName(val) {
  route.replaceQuery('name', val)
}

function userOperations(data) {
  return [OPERATIONS.check, OPERATIONS.modify, OPERATIONS.resetPassword]
}

const data = ref([])
const total = ref(0)

const config = {
  showIndex: true,
  operations: {
    label: '操作',
    width: 120,
    operations: userOperations
  },
  column: [
    {
      label: '头像',
      prop: 'avatar',
      type: COLUMN_TYPE.picture,
      width: 100,
    }, {
      label: '姓名',
      prop: 'name',
    }, {
      label: '账号',
      prop: 'loginName',
    }, {
      label: '用户状态',
      prop: 'flag',
      type: COLUMN_TYPE.switch,
      operationsKey: OPERATIONS.switch,
      activeText: '正常',
      inactiveText: '封禁',
      activeValue: 1, // 1正常 0封禁
      inactiveValue: 0,
      switchWidth: 55,
      width: 100
    }, {
      label: '归属基地',
      prop: 'shop',
      width: 100,
    }, {
      label: '用户角色',
      prop: 'roleName',
      width: 100,
    }, {
      label: '创建时间',
      prop: 'createTime',
      width: 100,
    }, {
      label: '创建人',
      prop: 'creator',
      width: 100,
    }
  ]
}

const opHander = operationsHander()
opHander.install(OP_KEY.check, (data) => {
  route.push({ name: 'userdetails', query: { type: 'show', code: data.id } })
})
opHander.install(OP_KEY.modify, (data) => {
  route.push({ name: 'userdetails', query: { type: 'modify', code: data.id } })
})
opHander.install(OP_KEY.resetPassword, (data) => {
  api.resetPassword(data.id, val => {
    $msg.succ("用户密码重置成功，新密码为123456！");
  })
})
opHander.install(OP_KEY.switch, (data) => {
  // 1正常 0封禁
  let status = data.data.flag == 1 ? 0 : 1
  api.status(data.data.id, status, () => {
    data.resolve(true)
    $msg.succ("用户状态修改成功");
    getUserList()
  }, () => {
    data.reject()
  })
})

function addUser() {
  route.push({ name: 'userdetails', query: { type: 'add' } })
}

getUserList()
function getUserList() {
  let routeQuery = route.query()
  api.userList({
    flag: routeQuery.status,
    shopId: routeQuery.base,
    loginName: routeQuery.name,
    page: routeQuery.p,
    size: routeQuery.s
  }, val => {
    total.value = val.total
    data.value = val.itemList
    statusData([val.allNum, val.normalNum, val.closeNum])
  })
}

function statusData(val) {
  statusList.forEach((e, i) => { e.total = val[i] })
}

function userExport() {
  let routeQuery = route.query()
  api.export({
    flag: routeQuery.status,
    shopId: routeQuery.base,
    loginName: routeQuery.name,
  }, val => {
    $.download(val.url, "用户导出")
  })
}
</script>